package com.haiyuancloud.base.menu.service.impl;

import com.haiyuancloud.common.core.entity.router.RouterMeta;
import com.haiyuancloud.common.core.entity.router.VueRouter;
import com.haiyuancloud.common.core.entity.system.Menu;
import com.haiyuancloud.common.core.entity.system.TbOrgMenu;
import com.haiyuancloud.common.core.utils.TreeUtil;
import com.haiyuancloud.base.menu.service.IMenuService;
import com.haiyuancloud.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service("com.haiyuancloud.base.menu.service.MenuService")
@RequiredArgsConstructor
public class MenuServiceImpl implements IMenuService {

    public List<Menu> findUserMenus(List<TbOrgMenu> tbOrgMenuList) {
        List<Menu> menuList = new ArrayList<>();
        tbOrgMenuList.forEach( tbOrgMenu ->{
            if(1 == tbOrgMenu.getIMenuType()) {
                Menu menu = new Menu();
                menu.setComponent(tbOrgMenu.getSMenuComp());
                menu.setIcon(tbOrgMenu.getSMenuIcon());
                menu.setMenuName(tbOrgMenu.getSMenuName());
                menu.setPerms(tbOrgMenu.getSAuthCode());
                menu.setPath(ObjectUtil.toString(tbOrgMenu.getSMenuUrl(),""));
                menu.setOrderNum(tbOrgMenu.getIMenuSort());
                menu.setMenuVisible(tbOrgMenu.getIMenuVisible());
                try {
                    menu.setParentId(Long.parseLong(tbOrgMenu.getSPid()));
                    menu.setMenuId(Long.parseLong(tbOrgMenu.getSId()));
                    menuList.add(menu);
                } catch (Exception e) {
                    log.error("转换类型异常," + e.getMessage());
                }
            }
        });
        List<Menu> menuNewList = menuList.stream().distinct().sorted((a, b) -> a.getOrderNum().compareTo(b.getOrderNum())).collect(Collectors.toList());
        menuList.clear();
        return menuNewList;
    }

    public List<VueRouter<Menu>> getUserRouters(List<TbOrgMenu> tbOrgMenuList) {
        List<VueRouter<Menu>> routes = new ArrayList<>();
        List<Menu> menus = this.findUserMenus(tbOrgMenuList);
        menus.forEach(menu -> {
            VueRouter<Menu> route = new VueRouter<>();
            route.setId(menu.getMenuId().toString());
            route.setParentId(menu.getParentId().toString());
            route.setPath(ObjectUtil.toString(menu.getPath(),""));
            route.setComponent(menu.getComponent());
            route.setName(menu.getMenuName());
            Boolean fixtitle = false;
            if(null != menu.getPath() && menu.getPath().contains("/ui/")){
                fixtitle = true;
            }
            route.setMeta(new RouterMeta(menu.getMenuName(), menu.getIcon(), true,false, fixtitle));
            if(!(ObjectUtil.isNotNull( menu.getMenuVisible()) &&
                    2 == menu.getMenuVisible())){
                routes.add(route);
            }
        });
        return TreeUtil.buildVueRouter(routes);
    }

    public List<String> getPathIdList(List<TbOrgMenu> tbOrgMenuList){
        List<String> pathIdList = new ArrayList<>();
        tbOrgMenuList.stream().map(TbOrgMenu :: getSPathId).map(pathId -> pathId.split("/")).forEach(menu -> {
            pathIdList.addAll(Arrays.asList(menu));
        });
        return pathIdList.stream().distinct().collect(Collectors.toList());
    }

    public Map<String,String> getIdData(List<TbOrgMenu> tbOrgMenuList){
        Map<String,String> map = new HashMap<>();
        List<String> idList = tbOrgMenuList.stream().map(TbOrgMenu :: getSId).distinct().collect(Collectors.toList());
        idList.forEach(id -> {
            map.put(id,id);
        });
        return map;
    }
}
